$(function(){

    var page = 1;
    var pageSize = 10;
    var totalPage = 0
   
    getData();

    $("#next").on("click",function(){
        page++;
        if(page > totalPage){
            page = totalPage;
            alert("已经是最后一页了");
            return;
        }
        getData();
    });

    $("#prev").on("click",function(){
        page--;
        if(page < 1){
            page = 1;
            alert("已经是第一页了");
            return;
        }
        getData();
    });

    function getData(){
        $.ajax({
            type: "get",
            url: "/category/queryTopCategoryPaging",
            data: {
                page: page,
                pageSize : pageSize, 
            },
            success: function (res) {
                console.log(res);
                totalPage = Math.ceil(res.total / pageSize);
                var html = template("categoryFirstTpl",res);
                $("#categoryFirstBox").html(html);
            }
        });
    }

    $("#save").on("click",function(){
        var categoryFirstName = $.trim($("[name='categoryFirstName']").val());
        console.log(categoryFirstName);
        if(!categoryFirstName){
            alert("请输入一级分类名称");
            return;
        }

        $.ajax({
            type: "post",
            url: "/category/addTopCategory",
            data: {
                categoryName : categoryFirstName,
            },
            success: function (res) {
                console.log(res);
                if(res.success){
                    location.reload();
                }
            }
        });

    });

});